# 字符串
# 原始字符串r R
s1 = '\it \is \time \to \read \now'
s2 = r'\it \is \time \to \read \now'
print(s1)
print(s2)
# 字符的特殊表示
s1 = '\141\142\143\x61\x62\x63'
s2 = '\u9a86\u660a'
print(s1)
print(s2)
# 字符的运算
# 字符串的拼接和重复
s1 = 'hello' + ', ' + 'world'
print(s1)    # hello, world
s2 = '!' * 3
print(s2)    # !!!
s1 += s2
print(s1)    # hello, world!!!
s1 *= 2
print(s1)    # hello, world!!!hello, world!!!
# 字符串的比较
s1 = 'a whole new world'
s2 = 'hello world'
print(s1 == s2)             # False
print(s1 < s2)              # True
print(s1 == 'hello world')  # False
print(s2 == 'hello world')  # True
print(s2 != 'Hello world')  # True
# ord()方法获取字符编码值
s3 = '骆昊'
print(ord('骆'))            # 39558
print(ord('昊'))            # 26122
s4 = '王大锤'
print(ord('王'))            # 29579
print(ord('大'))            # 22823
print(ord('锤'))            # 38180
print(s3 >= s4)             # True
print(s3 != s4)             # True
# 字符串的成员运算
s1 = 'hello, world'
s2 = 'goodbye, world'
print('wo' in s1)      # True
print('wo' not in s2)  # False
print(s2 in s1)        # False
# 获取字符串长度
s = 'hello, world'
print(len(s))                 # 12
print(s.__len__())
print(len('goodbye, world'))  # 14
# 字符串的索引和切片
s = 'abc123456'
n = len(s)
print(s[0], s[-n])    # a a
print(s[n-1], s[-1])  # 6 6
print(s[2], s[-7])    # c c
print(s[5], s[-4])    # 3 3
print(s[2:5])         # c12
print(s[-7:-4])       # c12
print(s[2:])          # c123456
print(s[:2])          # ab
print(s[::2])         # ac246
print(s[::-1])        # 654321cba
# 字符的遍历
a = "qweqicwievn"
for i in a:
    print(i,end="\t")

for i in range(len(a)):
    print(a[i])

# 字符串的方法
# 大小写操作
s1 = 'hello, world!'
print(s1.capitalize()) # 字符串首字母大写
print(s1.title()) # 字符串每个单词首字母大写
print(s1.upper()) # 字符串全部大写
s2 = 'GOODBYE'
print(s2.lower()) # 字符串全部转小写
# 字符串不可变，大小写操作后不改变原有值，只会生成新字符串
print(s1)
print(s2)
# 字符串查找
s = 'hello, world!'
print(s.find('or'))      # 找到后返回索引8
print(s.find('or', 5))   # 可以从指定索引开始查找
print(s.find('of'))      # 找不到字符串返回-1
print(s.index('or'))     # 8
# print(s.index('or', 9))  # 找不到报错ValueError: substring not found
print(s.rfind('o'))      # 逆向查找7
print(s.rindex('o'))     # 逆向查找7
# print(s.rindex('o', 8))  # ValueError: substring not found

# 性质判断
s1 = 'hello, world!'
print(s1.startswith("He")) # 是否以“he” 开头 False
print(s1.startswith("he")) # 是否以“he” 开头 True
print(s1.endswith("!")) # 是否以“！”结尾
s2 = 'abc123456'
print(s2.isdigit()) # 判断是否全部由数字构成
print(s2.isalpha()) # 判断是否全部由字母构成
print(s2.isalnum()) # 判断是否全部由字母和数字构成

# 字符串格式化
s = 'hello, world'
print(s.center(20,"#")) # 字符串居中，两端由#补足20位
print(s.rjust(20)) # 字符串靠右，左边由空格补足20位
print(s.ljust(20,"~")) # 字符串靠左，右边由~补足20位
print("ww".zfill(5)) # 字符串左侧补0补足5位 000ww
print("-33".zfill(6)) #  -00033
print("++++".zfill(6)) # +00+++
# 字符串赋值
a = 123
b = 321
print("{0}*{1}={2}".format(a,b,a*b)) # 123*321=39483
print("%d * %d = %d" % (a,b,a*b)) # 123 * 321 = 39483
print(f"{a}*{b}={a*b}") # 123*321=39483
print(f"{a}*{b}={a*b:.2f}") # 123*321=39483.00
print(f"{a}*{b}={a*b:.0f}") # 123*321=39483
print(f"{a}*{b}={a*b:+.2f}") # 123*321=+39483.00
print(f"{a}*{b}={a*b*-1:+.2f}") # 123*321=-39483.00
print(f"{a}*{b}={a*b*-1:0>10d}") # 123*321=0000-39483
print(f"{a}*{b}={a*b:x<10d}") # 123*321=39483xxxxx
print(f"{a}*{b}={a*b*-1:>10d}") # 123*321=    -39483
print(f"{a}*{b}={a*b:<10d}") # "123*321=39483     "
print(f"{a}*{b}={a*b:,}") # 39,483
print(f"{a}*{b}={a*b:.2%}") # 3948300.00%
print(f"{a}*{b}={a*b:.2e}") # 3.95e+04

# 修剪字符串
s1 = '   jackfrued@126.com  '
print(s1.strip()) # jackfrued@126.com
s2 = '~你好，世界~'
print(s2.lstrip("~")) # 你好，世界~
print(s2.rstrip("~")) # ~你好，世界
# 字符串替换
s = 'hello, good world'
print(s.replace("o","@")) #  hell@, g@@d w@rld
print(s.replace("o","@",1)) # hell@, good world
print(s.replace("o","@",2)) # hell@, g@od world
# 字符串的拆分与合并
s = 'I love you'
print(s.split()) #  ['I', 'love', 'you']
print("@".join(s.split())) # I@love@you
s = 'I#love#you#so#much'
print(s.split("#")) # ['I', 'love', 'you', 'so', 'much']
print(s.split("#",2)) # ['I', 'love', 'you#so#much']

# 字符串的编码和解码
name = "印志安"
a = name.encode("utf-8")
b = name.encode("gbk")
print(a) # b'\xe5\x8d\xb0\xe5\xbf\x97\xe5\xae\x89'
print(b) # b'\xd3\xa1\xd6\xbe\xb0\xb2'
print(a.decode("utf-8")) # 印志安
# print(a.decode("gbk")) # 报错 Traceback (most recent call last):
print(b.decode("gbk")) # 印志安